package pers.lance.administrator.listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pers.lance.administrator.pojo.Customer;
import pers.lance.administrator.service.CustomerService;
import pers.lance.administrator.util.JsonUtil;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * @ Author     ：LILA3
 * @ Date       ：Created in 8:32 PM 7/30/2018
 */
@Component
public class CustomerQueueListener implements MessageListener {
    @Autowired
    CustomerService customerService;

    @Override
    public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            String jsonData = textMessage.getText();
            Customer customer = JsonUtil.json2pojo(jsonData, Customer.class);
            customerService.addCustomer(customer);
        } catch (JMSException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
